import React from 'react';
import { Input, Tooltip } from 'antd';
import { InfoCircleOutlined, CheckCircleOutlined, CloseCircleOutlined } from '@ant-design/icons';
import { Rule } from 'antd/es/form';
import { useIntl } from 'umi';

interface AddressInputProps {
  value?: string;
  onChange?: (value: string) => void;
  placeholder?: string;
  style?: React.CSSProperties;
}

const AddressInput: React.FC<AddressInputProps> = ({ 
  value = '', 
  onChange,
  placeholder,
  style 
}) => {
  const intl = useIntl();
  
  const validateAddress = (address: string): boolean => {
    const pattern = /^\d+\s+[\w\s]+,\s*[\w\s]+,\s*[A-Z]{2}\s*\d{5}(-\d{4})?$/;
    return pattern.test(address?.trim());
  };

  const isValid = validateAddress(value);

  return (
    <div style={{ position: 'relative', ...style }}>
      <Input
        value={value}
        onChange={(e) => onChange?.(e.target.value)}
        placeholder={placeholder || intl.formatMessage({ id: 'pages.addressInput.placeholder' })}
        suffix={
          <Tooltip 
            title={isValid 
              ? intl.formatMessage({ id: 'pages.addressInput.valid.tooltip' }) 
              : intl.formatMessage({ id: 'pages.addressInput.invalid.tooltip' })
            }
            placement="topRight"
          >
            {isValid ? (
              <CheckCircleOutlined style={{ color: '#52c41a' }} />
            ) : (
              <CloseCircleOutlined style={{ color: '#ff4d4f' }} />
            )}
          </Tooltip>
        }
      />
      {!isValid && value && (
        <div style={{ color: '#ff4d4f', fontSize: 12, marginTop: 4 }}>
          <InfoCircleOutlined /> {intl.formatMessage({ id: 'pages.addressInput.errorMessage' })}
        </div>
      )}
    </div>
  );
};

export default AddressInput;

// 表单验证规则
// export const addressRules: Rule[] = [
//   { 
//     required: true, 
//     message: intl.formatMessage({ id: 'validation.address.required' }) 
//   },
//   () => ({
//     validator(_, value) {
//       const pattern = /^\d+\s+[\w\s]+,\s*[\w\s]+,\s*[A-Z]{2}\s*\d{5}(-\d{4})?$/;
//       if (!value || pattern.test(value.trim())) {
//         return Promise.resolve();
//       }
//       return Promise.reject(new Error(intl.formatMessage({ id: 'validation.address.format' })));
//     },
//   }),
// ];